<?php
/**

 All rights Reserved, Designed By www.youyacao.com
 @Description:短视频
 @author:成都市一颗优雅草科技有限公司
 @version 优雅草松鼠聚合直播系统
 需要商业用途或者定制开发等可访问songshu.youyacao.com  联系QQ:2853810243 422108995 23625059584
 正版系统查询系统 zhengbancha.youyacao.com

*/

namespace app\admin\controller;

use \think\Db;
use \think\Request;
use \think\Image;

class Video extends Base
{
    public function upload_image(){
        $this->onlyAdmin();

        $file = request()->file('file');
        $oldfile = input("oldfile");
        if($file){
            //图片可以是20M以内
            $info = $file->validate(['size'=>20480000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public/uploads');
            if($info){
                $exif = exif_read_data(ROOT_PATH . 'public/uploads/'.$info->getSaveName());
                $image = Image::open(ROOT_PATH . 'public/uploads/'.$info->getSaveName());

                if(!empty($exif['Orientation'])) {
                    switch($exif['Orientation']) {
                        case 8:
                            $image = $image->rotate(-90);
                            break;
                        case 3:
                            $image = $image->rotate(180);
                            break;
                        case 6:
                            $image = $image->rotate(90);
                            break;
                    }
                }

                $width = $image->width();
                $height = $image->height();
                $_height = $width ? $height/$width * 400 : 0;
                $filepath = $info->getSaveName();
                $filepath = str_replace("\\","/",$filepath);
                $image->thumb(400, $_height)->save(ROOT_PATH . 'public/uploads/'.$filepath);

                if(strlen($oldfile) && strpos($oldfile,"uploads/") === 0){
                    @unlink(ROOT_PATH . 'public/'.$oldfile);//删除旧图片
                }
                $this->success(['filepath'=>config('storage_url').'uploads/'.$filepath]);

            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        $this->error("未上传图片");
    }

    public function upload_video(){
        $this->onlyAdmin();

        $file = request()->file('file');
        $oldfile = input("oldfile");
        if($file){
            //视频可以是200M以内
            $info = $file->validate(['size'=>204800000,'ext'=>'avi,mpg,mpeg,mpe,m1v,m2v,mpv2,mp2v,dat,ts,tp,tpr,pva,pss,mp4,m4v,m4p,m4b,3gp,3gpp,3g2,3gp2,ogg,mov,qt,amr,rm,ram,rmvb,rpm'])->move(ROOT_PATH . 'public/uploads');
            if($info){
                $filepath = $info->getSaveName();
                $filepath = str_replace("\\","/",$filepath);

                if(strlen($oldfile) && strpos($oldfile,"uploads/") === 0){
                    @unlink(ROOT_PATH . 'public/'.$oldfile);//删除旧视频
                }
                $this->success(['filepath'=>config('storage_url').'uploads/'.$filepath]);

            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        $this->error("未上传图片");
    }

    public function save_video()
    {
        $this->onlyAdmin();
        if (!request()->isPost()) $this->error('请求类型有误');
        $id = input("id");
        $video = Db::name("videos")->where("id", $id)->find();
        $title = input('title');
        $img = input('imageurl');
        $address = input('videourl');

        if (strlen($title) == 0) $this->error("请填写一个标题");
        if (strlen($img) == 0) $this->error("请填上传一张封面照片");
        if (strlen($address) == 0) $this->error("视频地址不能为空");
        if ($video) {
            $data = array();
            $data['title'] = $title;
            $data['img'] = $img;
            $data['address'] = $address;
            $data['update_time'] = time();
            if (Db::name("videos")->where("id",$video['id'])->update($data)) {
                $this->success(NULL, "视频更新成功");
            } else {
                $this->error("视频更新未成功");
            }
        } else {
            $data = array();
            $data['title'] = $title;
            $data['img'] = $img;
            $data['address'] = $address;
            $data['create_time'] = time();
            if (Db::name("videos")->insert($data)) {
                $this->success(NULL, "视频发布成功");
            } else {
                $this->error("视频发布未成功");
            }
        }
    }

    public function getlist(){
        $this->onlyAdmin();

        $db = Db::name("videos")->field('id,title,img,create_time,update_time,collect_times');

        $keyword = input("keyword");
        if(strlen($keyword)){
            $db = $db->where("title","like","%".addslashes($keyword)."%");
        }

        $db = $db->order("id","desc");

        $list = $db->paginate(10);

        $list->each(function ($item,$key){
            $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);
            $item['update_time'] = $item['update_time'] ? date('Y-m-d H:i:s',$item['update_time']) : '从未更新';
            return $item;
        });

        $this->success($list);
    }

    public function get_video(){
        $this->onlyAdmin();
        $video = Db::name("videos")->where("id",intval(input("id")))->find();
        if(!$video) $this->error("未找到相关视频");
        $this->success($video);
    }

    public function delete(){
        $this->onlyAdmin();
        $video = Db::name("videos")->where("id",intval(input("id")))->find();
        if(!$video) $this->error("未找到相关视频");
        if(Db::name("videos")->where("id",$video['id'])->delete()){
            $img = $video['img'];
            $address = $video['address'];
            if(strpos($img,"uploads/") === 0) @unlink($img);
            if(strpos($address,"uploads/") === 0) @unlink($address);
            $this->success(NULL,"删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}
